package com.pignest.hotel.ui.income;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class ViewAccountsWindow extends Window implements EventListener {

	/**
	 * 查看账目明细
	 */
	private static final long serialVersionUID = -2727648965498870663L;

	private IncomeService incomeService;
	private Listbox qLiving;
	private Listbox qState;
	private Listbox aListbox;
	private Label usedAll;
	private Listbox accoutsListbox;
	private Radiogroup sGroup;
	private List<Living> selectLive;// 选择的入住

	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			qLiving = (Listbox) getFellow("qLiving");
			aListbox = (Listbox) getFellow("aListbox");
			usedAll = (Label) getFellow("usedAll");
			accoutsListbox = (Listbox) getFellow("accoutsListbox");
			qState = (Listbox) getFellow("qState");
			sGroup = (Radiogroup) getFellow("sGroup");
			init();
			qLiving.addEventListener(Events.ON_SELECT, new EventListener() {
				public void onEvent(Event event) {
					String selectId = UIUtils.getSelectedValue(qLiving);
					String state = UIUtils.getSelectedValue(qState);
					selectCurLivings(selectId, state);
				}
			});
			sGroup.addEventListener(Events.ON_CHECK, new EventListener() {
				public void onEvent(Event event) {
					String selectId = UIUtils.getSelectedValue(qLiving);
					String state = UIUtils.getSelectedValue(qState);
					initStatic(selectId, state);
				}
			});
			qState.addEventListener(Events.ON_SELECT, new EventListener() {
				public void onEvent(Event event) {
					String selectId = UIUtils.getSelectedValue(qLiving);
					String state = UIUtils.getSelectedValue(qState);
					selectCurLivings(selectId, state);
				}
			});
		}
	}

	public ViewAccountsWindow() {
		incomeService = Services.getIncomeService();
		addEventListener("onCreate", this);
	}

	public void init() {
		initSelectLiving();
		String selectId = UIUtils.getSelectedValue(qLiving);
		String state = UIUtils.getSelectedValue(qState);
		initStatic(selectId, state);
		queryAccounts(selectId, state);
		queryAllUsed(selectId, state);
	}

	public void selectCurLivings(String id, String state) {
		initStatic(id, state);
		queryAccounts(id, state);
		queryAllUsed(id, state);
	}

	/** 初始化下拉列表 */
	public void initSelectLiving() {
		if (selectLive != null) {
			qLiving.getItems().clear();
			Listitem listitemNull = new Listitem();
			listitemNull.setParent(qLiving);
			listitemNull.setValue("");
			listitemNull.setLabel("所有");
			listitemNull.setSelected(true);
			for (Living l : selectLive) {
				Listitem listitem = new Listitem();
				listitem.setParent(qLiving);
				listitem.setValue(l.getId());
				listitem.setLabel("[" + l.getRoom().getName() + "]"
						+ l.getCustormName());
			}
		}

	}

	/** 初始化统计项目 */
	public void initStatic(String livingId, String state) {
		aListbox.getItems().clear();
		List sl = null;
		String s = sGroup.getSelectedItem().getValue();
		int type = Integer.valueOf(s);
		if (!livingId.equals("")) {
			sl = incomeService.queryAccStaticByLiving(livingId, type, state);
		} else {
			sl = incomeService.queryAccStaticByMulLiving(selectLive, type,
					state);
		}
		if (sl == null)
			return;
		for (int i = 0; i < sl.size(); i++) {
			Object[] o = (Object[]) sl.get(i);
			String value = (String) o[1];
			String name = "";
			if (type == 1)
				name = InComeItemType.valueOf(value).getName();
			else
				name = value;
			Long d = (Long) o[0];
			Listitem listitem = new Listitem();
			listitem.setParent(aListbox);
			listitem.appendChild(new Listcell(MoneyUtil.toString(-d)));
			listitem.appendChild(new Listcell(name));
		}
	}

	public void queryAccounts(String livingId, String state) {
		List<TbAccounts> list = null;
		if (!livingId.equals("")) {
			list = incomeService.queryAllAccountUsedList(livingId, state);
		} else {
			list = incomeService.queryAllAccountUsedList(selectLive, state);
		}
		accoutsListbox.getItems().clear();
		if (list == null)
			return;

		int aIndex = 1;
		for (TbAccounts ac : list) {
			Listitem listitem = new Listitem();
			listitem.setValue(ac);
			listitem.setParent(accoutsListbox);
			listitem.appendChild(new Listcell(String.valueOf(aIndex)));
			listitem.appendChild(new Listcell(ac.getLiving().getRoom()
					.getName()));
			listitem.appendChild(new Listcell(ac.getName()));
			listitem.appendChild(new Listcell(InComeItemType.valueOf(
					ac.getType()).getName()));
			listitem.appendChild(new Listcell(Utils.toPrice(-ac.getPrice())));
			listitem.appendChild(new Listcell(Utils.toPrice(ac.getPrice2())));
			listitem.appendChild(new Listcell(String.valueOf(ac.getCounts())));
			listitem.appendChild(new Listcell(ac.getEndstate()));

			listitem.appendChild(new Listcell(DateUtil.toString(ac.getOptime(),
					DateUtil.HOTEL_DEFAULT_FORMAT)));
			if (ac.getPrice() >= 0)
				listitem.setCheckable(false);
			listitem.appendChild(new Listcell(LivingState.valueOf(ac.getLiving().getLiveState()).getDisplayName()));
			aIndex++;
		}
	}

	public void queryAllUsed(String livingId, String state) {
		int used = 0;
		if (!livingId.equals("")) {
			Living l = incomeService.get(Living.class, livingId);
			used = incomeService.countAllConsumsByState(l, state);
		} else {
			used = incomeService.countAllConsumsByState(selectLive, state);
		}
		usedAll.setValue(Utils.toPrice(used));
	}

	public List<Living> getSelectLive() {
		return selectLive;
	}

	public void setSelectLive(List<Living> selectLive) {
		this.selectLive = selectLive;
	}

}
